from django.shortcuts import render

from celery import task
from yisheng.models import User_Sites, User, Contract
from company_manage.models import YsbCompanyInfo, BaiduAppCompanyInfo, ClarkiiSeoCompanyInfo
from clarkii_seo.models import SeoSysUser, SeoUserBusiness
from baidu_app.models import Company_Backup
import re
from django.http import JsonResponse, HttpResponse



def GetCompanyInfo(request):
    phone_pat = re.compile('^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\\d{8}$')
    print("获取易数宝公司资料开始")
    SaveYsb(phone_pat)
    print("获取易数宝公司资料结束")
    print("获取百度商业平台公司信息开始")
    SaveBaiduApp(phone_pat)
    print("获取百度商业平台公司信息结束")
    print("获取seo公司信息开始")
    SaveClsrkiiSeo(phone_pat)
    print("获取seo公司信息结束")
    return JsonResponse({'resu': "success"})


def SaveYsb(phone_pat):
    userList = User.objects.using('formal_ysb').values('id', 'company_name', 'mobile')
    mobileStr = ""
    saveList = []
    for user in userList:
        contract = Contract.objects.using('formal_ysb').values('sign_date', 'expire_date', 'service_cycle',
                                                               'service_price',
                                                               'ad_online_date', 'ad_offline_date').filter(
            user_id_back=user['id'])
        ysbCompanyInfo = YsbCompanyInfo()
        ysbCompanyInfo.user_id = user['id']
        if (user['company_name'] != None):
            if ("原昇" in user['company_name'] or "测试" in user['company_name'] or "test" in user['company_name']):
                continue
            else:
                ysbCompanyInfo.company_name = user['company_name']
        if (user['mobile'] == None):
            continue
        elif (user['mobile'] != None):
            res = re.search(phone_pat, user['mobile'].strip().replace("\n", "").replace("\t", ""))
            if res == None:
                continue
            if(user['mobile'] in mobileStr):
                continue
            mobileStr = mobileStr + user['mobile'] + ','
            ysbCompanyInfo.mobile = user['mobile'].strip().replace("\n", "").replace("\t", "")
        if (len(contract) > 0):
            for c in contract:
                ysbCompanyInfo.sign_date = c['sign_date']
                if (c['expire_date'] != None):
                    ysbCompanyInfo.expire_date = c['expire_date']
                    ysbCompanyInfo.service_cycle = c['service_cycle']
                    ysbCompanyInfo.service_price = c['service_price']
                if (c['ad_online_date'] != None):
                    ysbCompanyInfo.ad_online_date = c['ad_online_date']
                if (c['ad_offline_date'] != None):
                    ysbCompanyInfo.ad_offline_date = c['ad_offline_date']

        saveList.append(ysbCompanyInfo)
    YsbCompanyInfo.objects.all().delete()
    result = YsbCompanyInfo.objects.bulk_create(saveList)
    return result


def SaveBaiduApp(phone_pat):
    saveList = []
    mobileStr = ""
    companyBackupList = Company_Backup.objects.using("formal_kad").all()
    for companyInfo in companyBackupList:
        if ("原昇" in companyInfo.company_name or "测试" in companyInfo.company_name or "test" in companyInfo.company_name):
            continue
        res = re.search(phone_pat, companyInfo.phone_number.strip().replace("\n", "").replace("\t", ""))
        if res == None:
            continue
        if (companyInfo.phone_number in mobileStr):
            continue
        mobileStr = mobileStr + companyInfo.phone_number + ','
        baiduAppCompanyInfo = BaiduAppCompanyInfo()
        baiduAppCompanyInfo.user_id = companyInfo.user_id
        baiduAppCompanyInfo.company_name = companyInfo.company_name
        baiduAppCompanyInfo.phone_number = companyInfo.phone_number.replace(' ', '').replace("\n", "")
        baiduAppCompanyInfo.create_time = companyInfo.create_time
        baiduAppCompanyInfo.visit_time = companyInfo.visit_time
        if (companyInfo.app_id == 1):
            baiduAppCompanyInfo.app_name = '原力提词'
        elif (companyInfo.app_id == 2):
            baiduAppCompanyInfo.app_name = '原力创意'
        elif (companyInfo.app_id == 3):
            baiduAppCompanyInfo.app_name = '原力防护'

        saveList.append(baiduAppCompanyInfo)
    BaiduAppCompanyInfo.objects.all().delete()
    BaiduAppCompanyInfo.objects.bulk_create(saveList)  # 批量存入


def SaveClsrkiiSeo(phone_pat):
    saveList = []
    mobileStr = ''
    seoUserBusiness = SeoUserBusiness.objects.using('formal_seo').all()
    for userBussiness in seoUserBusiness:
        seoSysUser = SeoSysUser.objects.using('formal_seo').filter(user_id=userBussiness.sys_user_id)
        if (len(seoSysUser) < 1):
            continue
        if (seoSysUser[0].mobile == "" or seoSysUser[0].mobile == None):
            continue
        if ("原昇" in userBussiness.company or "测试" in userBussiness.company or "test" in userBussiness.company):
            continue
        res = re.search(phone_pat, seoSysUser[0].mobile.strip().replace("\n", "").replace("\t", ""))
        if res == None:
            continue
        if (seoSysUser[0].mobile in mobileStr):
            continue
        mobileStr = mobileStr + seoSysUser[0].mobile + ','
        clarkiiSeoCompanyInfo = ClarkiiSeoCompanyInfo()
        clarkiiSeoCompanyInfo.user_id = userBussiness.sys_user_id
        clarkiiSeoCompanyInfo.company = userBussiness.company
        clarkiiSeoCompanyInfo.start_time = userBussiness.start_time
        clarkiiSeoCompanyInfo.end_time = userBussiness.end_time
        clarkiiSeoCompanyInfo.industry = userBussiness.industry
        clarkiiSeoCompanyInfo.mainBussiness = userBussiness.mainBussiness
        clarkiiSeoCompanyInfo.mobile = seoSysUser[0].mobile.strip().replace("\n", "").replace("\t", "")
        saveList.append(clarkiiSeoCompanyInfo)
    ClarkiiSeoCompanyInfo.objects.all().delete()
    ClarkiiSeoCompanyInfo.objects.bulk_create(saveList)
